package parser;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Vector;

import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import beans.Course;
import beans.Grade;
import beans.Instance;

public class GetPeriodosClaseParser {

	public Vector parseXML(String xml) {
		Vector instances = new Vector();
		Vector courses = new Vector();
		Vector grades = new Vector();
		// counters
		int cantPeriodo=0;
		int cantClase=0;
		int cantNota=0;
		try {
			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
			.parse(new ByteArrayInputStream(xml.getBytes("ISO-8859-1")));
			doc.getDocumentElement().normalize();
			NodeList list = doc.getElementsByTagName("*");
			
			//TODO remove old method
//			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//			DocumentBuilder builder = factory.newDocumentBuilder();
//			//xml = replace(xml, "i:nil=\"true\"", "");
//			InputStream inputStream = new ByteArrayInputStream(xml.getBytes("ISO-8859-1"));
//			Document document = builder.parse(inputStream);
//			Element rootElement = document.getDocumentElement();
//			rootElement.normalize();
//			NodeList list = document.getElementsByTagName("*");
			String node = new String();
			String element = new String();

			Instance instance = null;
			Course course = null;
			Grade grade = null;

			for (int i = 0; i < list.getLength(); i++) {

				if (list.item(i).hasChildNodes()){
					Node value = list.item(i).getChildNodes().item(0);
					node = list.item(i).getNodeName();
					element = value.getNodeValue();
					System.out.println("******************************************"+node+": "+element);

					// instance
					if(node.equals("Periodo")){
						if(cantPeriodo!=0){
							//problem: parseo 5 notas y agrego solo la ultima??
							// test
							//grades.addElement(grade);
							course.setGrades(grades);
							//
							// old uncomment
							// course.addGrade(grade);
							courses.addElement(course);
							instance.setCourses(courses);
							instances.addElement(instance);

							courses = new Vector();
							grades = new Vector();
							cantClase=0;
							cantNota=0;
						}
						System.out.println("******************************///// Periodo "+cantPeriodo);
						Element elem = (Element) list.item(i);
						instance = parseInstance(elem);
						cantPeriodo++;
					} else
						// course
						if(node.equals("Clase")){
							if(cantClase!=0){
								// add the class
								course.setGrades(grades);
								// add course
								courses.addElement(course);
								grades = new Vector();
								cantNota=0;
							}
							System.out.println("******************************///// Clase "+cantClase);
							Element elem = (Element) list.item(i);
							course = parseCourse(elem);
							cantClase++;
						} else
							// grade
							if(node.equals("Nota")){
								System.out.println("******************************///// Nota "+cantNota);
								Element elem = (Element) list.item(i);
								grade = parseGrade(elem);
								grades.addElement(grade);
								cantNota++;
							} 
				}

			}
			if (instance!=null){
				// grades
				course.setGrades(grades);
				// add last course
				courses.addElement(course);
				instance.setCourses(courses);
				instances.addElement(instance);
			}
		}

		catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		}

		System.out.println(" --------------- DONE ");
		return instances;
	}

	private Grade parseGrade(Element elem) {
		return new Grade(ParserMethods.getTextValue(elem,"Nombre"),ParserMethods.getTextValue(elem, "Valor"));
	}

	private Course parseCourse(Element elem) {
		Course co = new Course();
		String nroAula = ParserMethods.getTextValue(elem, "NumeroAula");
		if(nroAula==null){
			nroAula="-";
		}
		co.setIDAula(ParserMethods.getTextValue(elem, "IDAula"));
		co.setName(ParserMethods.getTextValue(elem, "NombreMateria"));
		co.setDays(ParserMethods.getTextValue(elem, "DiasCursada"));
		co.setTime(ParserMethods.getTextValue(elem, "Horario"));
		co.setClassroom(nroAula);
		co.setExamDate(ParserMethods.getTextValue(elem, "FechaExamen").substring(0,10));
		co.setAttendance(ParserMethods.getTextValue(elem, "Asistencia"));
		return co;
	}

	private Instance parseInstance(Element elem) {
		return new Instance(ParserMethods.getTextValue(elem,"Nombre"));
	}
}
